<?php
namespace Publish\Controller;
use Common\Controller\AdminbaseController;

class AdminInformationController extends AdminbaseController {
	protected $adminInformation_model;

	public function _initialize() {
		parent::_initialize();
		$this->adminInformation_model = D('PubInformation');
	}
	/*通过人物匹配其他资料*/
	public function informationList(){
		$person_id = I('person_id');
		$inforList = \Publish\Lib\InformationLib::GetInfor($person_id);
		$html = '';
		foreach($inforList as $key => $value){
			$html .= '<option value="'.$value['id'].'">'.$value['title'].'</option>';
		}
		echo $html;
	}

	public function index(){
		$this->_lists();
	}

	public function add(){
		$this->display();
	}

	public function add_post(){
		if(IS_POST){
			empty($_POST['person_id']) && $this->error("请选择人物");
			/*if(!empty($_POST['photos_url'])){ //是否有图片上传
				foreach ($_POST['photos_url'] as $key=>$url){
					$_POST['photos_url'][$key] = sp_asset_relative_url($url); //网络路径修复
				}
				//上传图集数量
				$num = count($_POST['photos_url']);
				for($i=0;$i<$num;++$i){
					//上传图集
					$add_image = \Publish\Lib\ImageLib::AddImages($_POST['person_id'],$_POST['photos_name'][$i],$_POST['photos_alt'][$i],$_POST['photos_url'][$i],'2');
					if(!$add_image) $this->error("上传失败");
				}
			}*/
			$data['person_id'] = $_POST['person_id'];
			$data['title'] = $_POST['post_title'];
			$data['content'] = $_POST['post_content'];
			$data['time'] = time();

			if($this->adminInformation_model->create($data)){
				if($this->adminInformation_model->add($data)){
					$this->success("添加成功");
				}else{
					$this->error("添加失败");
				}
			}
		}
	}

	public function edit(){
		$id = I('get.id','','intval');
		$information = \Publish\Lib\InformationLib::Information($id); //其他资料信息
		$image_arr = \Publish\Lib\ImageLib::GetImages($information['person_id'],'2'); //返回对应下的图片集合
		$this->assign($information);
		$this->assign('image_arr',$image_arr);
		$this->display();
	}

	public function edit_post(){
		if(IS_POST){
			empty($_POST['person_id']) && $this->error("请选择人物");
			$id = I('information_id','','intval'); //其他资料 ID
			$data['person_id'] = $_POST['person_id'];
			$data['title'] = $_POST['post_title'];
			$data['content'] = $_POST['post_content'];
			$data['time'] = time();
			/*修改其他资料库信息*/
			if($this->adminInformation_model->create($data)){
				if($this->adminInformation_model->where(array("id"=>$id))->save($data) !== false){
					$this->success("修改成功");
				}else{
					$this->error("修改失败");
				}
			}
		}
	}

	/**
	 * 根据不同条件显示
	 * @param array $where 查询条件
	 */
	private function _lists($where=array()){
		/*搜索条件*/
		$start_time=I('request.start_time');
		if(!empty($start_time)){
			$where['a.createtime']=array(
				array('EGT',strtotime($start_time))
			);
		}
		$end_time=I('request.end_time');
		if(!empty($end_time)){
			if(empty($where['a.createtime'])){
				$where['a.createtime']=array();
			}
			array_push($where['a.createtime'], array('ELT',strtotime($end_time)));
		}
		$keyword=I('request.keyword');
		if(!empty($keyword)){
			$where['a.title|b.name']=array('like',"%$keyword%");
		}

		/*分页--带条件查询分页*/
		$count = $this->adminInformation_model
			->alias("a")
			->join("__PUB_PERSON__ as b ON a.person_id = b.id")
			->where($where)
			->count();
		$page = $this->page($count, 100);
		$information = $this->adminInformation_model
			->alias("a")
			->join("__PUB_PERSON__ as b ON a.person_id = b.id")
			->field("a.*,b.name")
			->order('a.id asc,a.time desc')
			->where($where)
			->limit($page->firstRow , $page->listRows)
			->select();


		$this->assign("formget",array_merge($_GET,$_POST));
		$this->assign("page", $page->show('Admin'));
		$this->assign("information",$information);
		$this->display();
	}
	//删除
	public function delete(){
		if(isset($_GET['id'])){
			$id = I("get.id",0,'intval');
			$inforImageData = \Publish\Lib\ImageLib::InforImage($id);
			//判断是否有图片信息
			if(!empty($inforImageData)){
				//删除其他资料下的多个图片集合
				for($i=0;$i<count($inforImageData);$i++){
					$is_status = \Publish\Lib\ImiLib::UnlickFile($inforImageData[$i]['img_url']);
					$is_status || $this->error("删除失败！");
					$is_img_status = \Publish\Lib\ImageLib::del(array('infor_id'=>$id));
				}
			}
			if ($this->adminInformation_model->where(array('id'=>$id))->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}

		if(isset($_POST['ids'])){
			$ids = I('post.ids/a');
			for($i=0;$i<count($ids);$i++){
				$inforImageData = \Publish\Lib\ImageLib::InforImage($ids[$i]);
				for($a=0;$a<count($inforImageData);$a++){
				$is_status = \Publish\Lib\ImiLib::UnlickFile($inforImageData[$a]['img_url']);
					$is_status || $this->error("删除失败！");
			}
			}

			if ($this->adminInformation_model->where(array('id'=>array('in',$ids)))->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}
}